//
//  DetailViewController.m
//  BCProjectB
//
//  Created by lanou3g on 15/12/19.
//  Copyright © 2015年 lanou3g. All rights reserved.
//

#import "DetailViewController.h"
#import "BCListModel.h"
#import "BCReadDetailModel.h"
#import "DetailNetRequstHelper.h"
#import "DetailReadCell.h"
#import "WebViewController.h"
#import "MJRefresh.h"


@interface DetailViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)NSMutableArray *ListArray;
@property(nonatomic,strong)NSMutableArray *NewArray;
@property(nonatomic,strong)UIScrollView *contentScrollView;
@property(nonatomic,strong)UITableView *ListTableView;
@property(nonatomic,strong)UITableView *HotTableView;
@property(nonatomic,strong)MJRefreshHeader *header;
@property(nonatomic,strong)UISegmentedControl *segment;
@end
static NSInteger flage=0;
static NSString *strA=@"hot";
static NSString *strB=@"addtime";
static NSString *reusedId=@"hot";
@implementation DetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
    [self requstDataWith:strA];
    [self requstDataWith:strB];
     [self updataUI];
   //上拉刷新
    //下拉加载
    self.ListTableView.header=[MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(reaustNewData)];
    self.ListTableView.footer=[MJRefreshAutoFooter footerWithRefreshingTarget:self refreshingAction:@selector(requstMoreData)];
    self.HotTableView.header=[MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(requstNewDataForNew)];
    self.HotTableView.footer=[MJRefreshAutoFooter footerWithRefreshingTarget:self refreshingAction:@selector(requstMoreDataForNew)];
}
-(NSMutableArray*)ListArray{
    if (!_ListArray) {
        _ListArray=[NSMutableArray array];
    }
    return _ListArray;
}
-(NSMutableArray *)NewArray{
    if (!_NewArray) {
        _NewArray=[NSMutableArray array];
    }
    return _NewArray;
}
-(void)requstNewDataForNew{
    if ([DetailNetRequstHelper checkNetIsConectOrNot]==NO) {
        [self.HotTableView.header endRefreshing];
        UIAlertController *ac=[UIAlertController alertControllerWithTitle:@"提示信息" message:@"没网了" preferredStyle:(UIAlertControllerStyleAlert)];
        UIAlertAction *aa=[UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:nil];
        [ac addAction:aa];
        
    }
    DetailNetRequstHelper *helper=[[DetailNetRequstHelper alloc]init];
    
    __weak typeof(self) WeakSelf =self;
    
    [helper requstReadListView:^(id objc) {
        [WeakSelf.ListTableView reloadData];
        [_HotTableView.header endRefreshing];
    } with:@"hot" with:@"0"and:self.listModel.type];
    
}
-(void)requstMoreDataForNew{
    if ([DetailNetRequstHelper checkNetIsConectOrNot]==NO) {
        [self.HotTableView.header endRefreshing];
        UIAlertController *ac=[UIAlertController alertControllerWithTitle:@"提示信息" message:@"没网了" preferredStyle:(UIAlertControllerStyleAlert)];
        UIAlertAction *aa=[UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:nil];
        [ac addAction:aa];
        
    }
    DetailNetRequstHelper *helper=[[DetailNetRequstHelper alloc]init];
    
    __weak typeof(self) WeakSelf =self;
    flage+=10;
    [helper requstReadListView:^(id objc) {
        NSArray *arr=(NSMutableArray *)objc;
        [self.NewArray addObjectsFromArray:arr];
        [WeakSelf.HotTableView reloadData];
        [_HotTableView.footer endRefreshing];
    } with:@"addtime" with:[NSString stringWithFormat:@"%ld",flage] and:self.listModel.type];
}
-(void)reaustNewData{
    if ([DetailNetRequstHelper checkNetIsConectOrNot]==NO) {
        UIAlertController *alert=[[UIAlertController alloc]initWithNibName:@"网络信息" bundle:nil];
        UIAlertAction *action=[UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:nil];
        [alert addAction:action];
    }
    DetailNetRequstHelper *helper=[[DetailNetRequstHelper alloc]init];
    
    __weak typeof(self) WeakSelf =self;
    
    [helper requstReadListView:^(id objc) {
            [WeakSelf.ListTableView reloadData];
        [_ListTableView.header endRefreshing];
    } with:@"hot" with:@"0"and:self.listModel.type];

}
-(void)requstMoreData{
    DetailNetRequstHelper *helper=[[DetailNetRequstHelper alloc]init];
    
    __weak typeof(self) WeakSelf =self;
    flage+=10;
    [helper requstReadListView:^(id objc) {
        NSArray *arr=(NSMutableArray *)objc;
        [self.ListArray addObjectsFromArray:arr];
                [WeakSelf.ListTableView reloadData];
        [_ListTableView.footer endRefreshing];
    } with:@"hot" with:[NSString stringWithFormat:@"%ld",flage] and:self.listModel.type];
    

}
-(void)updataUI{
    //segamet
    NSArray *arr=@[@"Hot",@"New"];
    self.segment=[[UISegmentedControl alloc]initWithItems:arr];
    self.segment.frame=CGRectMake(self.view.width/2-70, 84, 140, 30);
   self.segment.selectedSegmentIndex = 0;
    //设置未选中时背景颜色
   self.segment.backgroundColor = [UIColor whiteColor];
    //设置边框颜色(字体)
   self.segment.tintColor = [UIColor colorWithRed:29/255.0 green:168/255.0 blue:74/255.0 alpha:1];

    [self.view addSubview:self.segment];
    [self.segment addTarget:self action:@selector(SegametAction:) forControlEvents:(UIControlEventValueChanged)];
    //scrollveiw
    _contentScrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 140, self.view.width, self.view.height)];
    _contentScrollView.contentSize=CGSizeMake(self.view.width*2,self.view.height-120);
       _ListTableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height-160) style:(UITableViewStylePlain)];
    [self.contentScrollView addSubview:_ListTableView];
    self.contentScrollView.pagingEnabled=YES;
    _HotTableView=[[UITableView alloc]initWithFrame:CGRectMake(self.view.width,0, self.view.width, self.view.height-160)style:(UITableViewStylePlain)];
      [self.contentScrollView addSubview:_HotTableView];
    [self.ListTableView registerNib:[UINib nibWithNibName:@"DetailReadCell" bundle:nil] forCellReuseIdentifier:reusedId];
    [self.HotTableView registerNib:[UINib nibWithNibName:@"DetailReadCell" bundle:nil] forCellReuseIdentifier:reusedId];
    self.automaticallyAdjustsScrollViewInsets=NO;
    [self.view addSubview:_contentScrollView];
    self.contentScrollView.delegate=self;
    self.ListTableView.dataSource=self;
    self.ListTableView.delegate=self;
    self.HotTableView.delegate=self;
    self.HotTableView.dataSource=self;
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    self.segment.selectedSegmentIndex=self.contentScrollView.contentOffset.x/self.view.width;
}
-(void)SegametAction:(UISegmentedControl *)sender{
    if (sender.selectedSegmentIndex==0) {
        [UIView animateWithDuration:0.5 animations:^{
            self.contentScrollView.contentOffset=CGPointMake(0, 0);
        } completion:^(BOOL finished) {
            
        }];

    }else if(sender.selectedSegmentIndex==1){
        [UIView animateWithDuration:0.5 animations:^{
            self.contentScrollView.contentOffset=CGPointMake(self.view.width, 0);
        } completion:^(BOOL finished) {
            
        }];

    }
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if ([tableView isEqual:_ListTableView]) {
        return self.ListArray.count;
 
    }
    return self.NewArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DetailReadCell *cell=[tableView dequeueReusableCellWithIdentifier:reusedId forIndexPath:indexPath];
    if ([tableView isEqual:self.HotTableView]) {
       BCReadDetailModel *model=self.NewArray[indexPath.row];
        [cell upDataUIwith:model];
        return cell;
    }
    BCReadDetailModel *model=self.ListArray[indexPath.row];
    [cell upDataUIwith:model];
    return cell;
  
}
-(void)requstDataWith:(NSString *)type {
    if ([DetailNetRequstHelper checkNetIsConectOrNot]==NO) {
        UIAlertController *alerVc=[UIAlertController alertControllerWithTitle:@"网络监测" message:@"没网额" preferredStyle:(UIAlertControllerStyleAlert)];
        UIAlertAction *alerAcrtion=[UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:nil];
        [alerVc addAction:alerAcrtion];
        [self presentViewController:alerVc animated:YES completion:nil];
    }
    DetailNetRequstHelper *helper=[[DetailNetRequstHelper alloc]init];
   
    __weak typeof(self) WeakSelf =self;
  
    [helper requstReadListView:^(id objc) {
       
        if ([type isEqualToString:@"hot"]) {
              self.ListArray=(NSMutableArray *)objc;
            [WeakSelf.ListTableView reloadData];
        }else if([type isEqualToString:@"addtime"]){
            self.NewArray=(NSMutableArray *)objc;
            [WeakSelf.HotTableView reloadData];
         
        }
    } with:type with:@"0"and:self.listModel.type];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 200;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    BCReadDetailModel *model=nil;
    if ([tableView isEqual:_ListTableView]) {
       model=self.ListArray[indexPath.row];
    }else{
        model=self.NewArray[indexPath.row];
    }
   
   NSString *url = [NSString stringWithFormat:@"http://pianke.me/webview/%@",model.ID];
    WebViewController *web=[[WebViewController alloc]init];
    web.url=url;
    web.IsHelp=YES;
    [self.navigationController pushViewController:web animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
